package cn.tannn.cat.file.server.entity;

import cn.tannn.cat.file.sdk.bean.FileStorage;
import cn.tannn.cat.file.sdk.config.Aws3Config;
import cn.tannn.cat.file.sdk.config.LocalConfig;
import cn.tannn.cat.file.sdk.config.MinioConfig;
import cn.tannn.cat.file.sdk.config.QiNiuConfig;
import cn.tannn.cat.file.sdk.enums.StorageMaster;
import cn.tannn.cat.file.server.entity.convert.JSONObjectConverter;
import com.alibaba.fastjson2.JSONObject;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

/**
 * 存储器配置
 *
 * @author <a href="https://tannn.cn/">tan</a>
 * @date 2024/3/7 11:05
 */
@Entity
@Table(name = "file_storage_config")
@Comment("存储器配置")
@Getter
@Setter
@ToString
@DynamicUpdate
@DynamicInsert
public class FileStorageConfig extends CommonBean<FileStorageConfig> {

    /**
     * 存储器名
     */
    @Comment("存储器名")
    @Column(columnDefinition = "varchar(100) not null")
    private String name;


    /**
     * 存储器字典值[Dict.value]
     */
    @Comment("存储器字典值[Dict.value]")
    @Column(columnDefinition = "varchar(100) not null")
    private String storage;


    /**
     * 主才能使用，从只是作为替补的
     * @see StorageMaster
     */
    @Comment("存储器状态[0从 1主]")
    @Column(columnDefinition = "int default 0")
    private StorageMaster master;

    /**
     * 存储配置Json
     * @see Aws3Config
     * @see LocalConfig
     * @see MinioConfig
     * @see QiNiuConfig
     */
    @Comment("存储配置Json")
    @Column(columnDefinition = "json not null")
    @Convert(converter = JSONObjectConverter.class)
    private JSONObject config;


    /**
     * 备注
     */
    @Comment("备注")
    @Column(columnDefinition = "varchar(100)")
    private String remark;


    /**
     * FileStorageConfig -> FileStorage
     */
    public FileStorage toFileStorage(){
        FileStorage fileStorage = new FileStorage();
        fileStorage.setId(getId());
        fileStorage.setName(name);
        fileStorage.setStorage(storage);
        fileStorage.setMaster(master);
        fileStorage.setConfig(config);
        fileStorage.setRemark(remark);
        return fileStorage;
    }

    /**
     * FileStorage -> FileStorageConfig
     */
    public static FileStorageConfig toFileStorage(FileStorage storage){
        FileStorageConfig storageConfig = new FileStorageConfig();
        storageConfig.setId(storage.getId());
        storageConfig.setName(storage.getName());
        storageConfig.setStorage(storage.getStorage());
        storageConfig.setMaster(storage.getMaster());
        storageConfig.setConfig(storage.getConfig());
        storageConfig.setRemark(storage.getRemark());
        return storageConfig;
    }

}
